#include "library_manager.h"

//--------------------------------------------------------------------------------------------
Library_manager::Library_manager(QWidget *parent) : QMainWindow(parent)
{
	ui.setupUi(this);
	this->setupConnections();
	this->initialize();
}

//--------------------------------------------------------------------------------------------
void Library_manager::setupConnections()
{
	connect(this->ui.tb_add_library, SIGNAL(clicked()), this, SLOT(add_library()));
	connect(this->ui.tb_remove_library, SIGNAL(clicked()), this, SLOT(remove_library()));
	connect(this->ui.pb_remove_piece, SIGNAL(clicked()), this, SLOT(remove_library_piece()));
	connect(this->ui.pb_edit_piece, SIGNAL(clicked()), this, SLOT(edit_library_piece()));
	connect(this->ui.pb_rename, SIGNAL(clicked()), this, SLOT(rename_library()));
	connect(this->ui.actionNueva, SIGNAL(triggered()), this, SLOT(add_library()));
	connect(this->ui.actionEliminar, SIGNAL(triggered()), this, SLOT(remove_library()));
	connect(this->ui.actionGuardar_copia, SIGNAL(triggered()), this, SLOT(save_library_copy()));
	connect(this->ui.actionSalir, SIGNAL(triggered()), this, SLOT(close()));
	connect(this->ui.lw_libraries, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(select_library()));
        //connect(this->centralWidget(), SIGNAL(), this, SLOT(refresh()));
}

//--------------------------------------------------------------------------------------------
void Library_manager::initialize()
{
        Library::LoadLibraryList(QCoreApplication::applicationDirPath () + "/librerias", this->ui.lw_libraries);
	this->ui.lw_pieces->clear();
}

//--------------------------------------------------------------------------------------------
void Library_manager::add_library()
{
	bool ok;
	QString filename = QInputDialog::getText(this, tr("Nueva Biblioteca"), tr("Nombre del archivo:"), QLineEdit::Normal, "", &ok);
	if (ok && !filename.isEmpty()){
		Library new_library;
                new_library.setPath(QCoreApplication::applicationDirPath () + "/librerias/" + filename + ".lib");
		QString name = QInputDialog::getText(this, tr("Nombre"), tr("Ingrese un nombre descriptivo:"), QLineEdit::Normal, "", &ok);
		if (ok && !name.isEmpty())
			new_library.setName(name);
		else
			new_library.setName("");
		new_library.saveLibrary();
	}
        Library::LoadLibraryList(QCoreApplication::applicationDirPath () + "/librerias", this->ui.lw_libraries);
}

//--------------------------------------------------------------------------------------------
void Library_manager::remove_library()
{
	QListWidgetItem *item = this->ui.lw_libraries->currentItem();
	if (item){
		QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Eliminar biblioteca"), "Desea realmente eliminar la biblioteca:" + item->text(),
		                                     QMessageBox::Yes | QMessageBox::No);
		if (reply == QMessageBox::Yes){
                        bool ok = QFile::remove(QCoreApplication::applicationDirPath () + "/librerias/" + item->text());
			if (!ok)
				QMessageBox::critical(this, tr("Error!"), tr("No se pudo eliminar el archivo."), QMessageBox::Ok);
                        Library::LoadLibraryList(QCoreApplication::applicationDirPath () + "/librerias", this->ui.lw_libraries);
		}
	}
}

//--------------------------------------------------------------------------------------------
void Library_manager::remove_library_piece()
{
	QListWidgetItem *item = this->ui.lw_pieces->currentItem();
	if (item){
		QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Eliminar pieza"), "Desea realmente eliminar la pieza seleccionada?",
		                                     QMessageBox::Yes | QMessageBox::No);
		if (reply == QMessageBox::Yes){
			Library library(this->ui.lb_library_path->text());
			library.removePieceAt(this->ui.lw_pieces->currentRow());
			library.saveLibrary();
			this->select_library();
		}
	}
}

//--------------------------------------------------------------------------------------------
void Library_manager::edit_library_piece()
{
	QListWidgetItem *item = this->ui.lw_pieces->currentItem();
	if (item){
		Editor *editor = new Editor(this->ui.lb_library_path->text(), this->ui.lw_pieces->currentRow());
		editor->show();
	}
}

//--------------------------------------------------------------------------------------------
void Library_manager::rename_library()
{
	bool ok;
	QString name = QInputDialog::getText(this, tr("Renombrar"), tr("Ingrese un nombre descriptivo:"), QLineEdit::Normal, this->ui.lb_name->text(), &ok);
	if (ok && !name.isEmpty()){
		Library library(this->ui.lb_library_path->text());
		library.setName(name);
		this->ui.lb_name->setText(name);
		library.saveLibrary();
	}
}

//--------------------------------------------------------------------------------------------
void Library_manager::save_library_copy()
{
	QListWidgetItem *item = this->ui.lw_libraries->currentItem();
	if (item){
		bool ok;
                QString copy_path = QCoreApplication::applicationDirPath () + "/librerias/" +
							QInputDialog::getText(this, tr("Guardar copia"), tr("Ingrese el nombre del archivo copia:"), QLineEdit::Normal, "copia de " + item->text(), &ok);
		if (ok && !copy_path.isEmpty()){
                        Library library(QCoreApplication::applicationDirPath () + "/librerias/" + item->text());
			library.saveLibraryAs(copy_path);
                        Library::LoadLibraryList(QCoreApplication::applicationDirPath () + "/librerias", this->ui.lw_libraries);
		}
	}
}

//--------------------------------------------------------------------------------------------
void Library_manager::select_library()
{
	QListWidgetItem *item = this->ui.lw_libraries->currentItem();
	if (item){
                this->ui.lb_library_path->setText(QCoreApplication::applicationDirPath () + "/librerias/" + item->text());
		Library library(this->ui.lb_library_path->text());
		this->ui.lb_name->setText(library.getName());
		this->ui.lb_count_pieces->setText(QVariant(library.count()).toString());
		Library::LoadPiecesList(this->ui.lb_library_path->text(), this->ui.lw_pieces);
	}
}

//--------------------------------------------------------------------------------------------
void Library_manager::refresh()
{
	this->initialize();
	this->select_library();
}

//--------------------------------------------------------------------------------------------
Library_manager::~Library_manager()
{

}
